//
//  BurseViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/4/8.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "BurseViewController.h"
#import "BuresResultViewController.h"
#import "ChargeResultTableViewController.h"
@interface BurseViewController ()
@property (nonatomic ,strong) UITextField *count;
@property (nonatomic,assign) NSInteger paymentType;
@property (nonatomic,strong) NSString *ordersn;

@end

@implementation BurseViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self showNav];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=MyString(@"钱包充值");
    [self exditView];
    // Do any additional setup after loading the view.
}

-(void)createView{
    //一
    UIView*view1=[[UIView alloc]init];
    view1.frame=CGRectMake(0, 0, MainW, 50);
    view1.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:view1];
    MYLabel *MLabel=[[MYLabel alloc]initWithFrame:CGRectMake(10, 15, 70, 20)];
    MLabel.textColor=TextColor3;
    //MLabel.backgroundColor=[UIColor redColor];
    MLabel.text=[NSString stringWithFormat:@"%@",MyString(@"充值金额")];
    UITextField*field=[[UITextField alloc]initWithFrame:CGRectMake(MLabel.frame.size.width+35, 15, MainW-MLabel.frame.size.width-35, 20)];
    field.placeholder=MyString(@"请填写充值金额");
    field.font=FontAndStyle14;
    field.keyboardType=UIKeyboardTypeNumberPad;
    self.count=field;
    field.borderStyle=0;
    [view1 addSubview:field];
    [view1 addSubview:MLabel];
    //二
    UIView*view2=[[UIView alloc]init];
    view2.frame=CGRectMake(0, 60, MainW, 50);
    view2.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:view2];
    UIImageView *WCImg=[[UIImageView alloc]initWithFrame:CGRectMake(10, 12.5, 25, 25)];
    WCImg.image=[UIImage imageNamed:@"icon_wechat"];
    //MLabel.backgroundColor=[UIColor redColor];
    MYLabel *WCLabel=[[MYLabel alloc]initWithFrame:CGRectMake(50, 15, MainW-50, 20)];
    WCLabel.textColor=TextColor3;
    //MLabel.backgroundColor=[UIColor redColor];
    WCLabel.text=[NSString stringWithFormat:@"%@",MyString(@"微信支付")];
    [view2 addSubview:WCLabel];
    [view2 addSubview:WCImg];
    UIButton *WCbtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 60, MainW, 50)];
    WCbtn.backgroundColor=[UIColor clearColor];
    [WCbtn addTarget:self action:@selector(WcChatClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:WCbtn];
    //三
    UIView*view3=[[UIView alloc]init];
    view3.frame=CGRectMake(0, 111, MainW, 50);
    view3.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:view3];
    UIImageView *ZBImg=[[UIImageView alloc]initWithFrame:CGRectMake(10, 12.5, 25, 25)];
    ZBImg.image=[UIImage imageNamed:@"icon_alipay1"];
    //MLabel.backgroundColor=[UIColor redColor];
    MYLabel *ZBLabel=[[MYLabel alloc]initWithFrame:CGRectMake(50, 15, MainW-50, 20)];
    ZBLabel.textColor=TextColor3;
    //MLabel.backgroundColor=[UIColor redColor];
    ZBLabel.text=[NSString stringWithFormat:@"%@",MyString(@"支付宝支付")];
    [view3 addSubview:ZBLabel];
    [view3 addSubview:ZBImg];
    UIButton *ZBbtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 111, MainW, 50)];
    ZBbtn.backgroundColor=[UIColor clearColor];
    [ZBbtn addTarget:self action:@selector(AlipayChatClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:ZBbtn];
    
}
#pragma  mark -----微信支付
-(void)WcChatClick{
    if (self.count.text.length==0) {
        [self showToast:@"请填写充值金额!"];
        return;
    }
    if ([self.count.text intValue]>=1000000) {
        [self showToast:@"金额超过最高限制!"];
        return;
    }
    self.paymentType=111;
    [self loadrechargeRequest];
}
#pragma  mark -----支付宝支付
-(void)AlipayChatClick{
    if (self.count.text.length==0) {
        [self showToast:@"请填写充值金额!"];
        return;
    }
    if ([self.count.text intValue]>=1000000) {
        [self showToast:@"金额超过最高限制!"];
        return;
    }
    self.paymentType=101;
    [self loadrechargeRequest];
    
}

-(void)loadrechargeRequest{
    NSMutableDictionary*dic=[NSMutableDictionary dictionary];
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    dic[@"amount"]=self.count.text;
    [self doHttpRequestWithApi:RECHARGE  method:GET dic:dic successBlock:^(NSDictionary *dic) {
        self.ordersn=[NSString stringWithFormat:@"%@",dic[@"data"]];
        [self paymentRequest:[NSString stringWithFormat:@"%@",dic[@"data"]]];
    }];
}
-(void)paymentRequest:(NSString *)orderSn{
    NSMutableDictionary*dic=[NSMutableDictionary dictionary];
    dic[@"payNo"]=orderSn;
    dic[@"paymentType"]=[NSString stringWithFormat:@"%ld",(long)self.paymentType];
    [self doHttpRequestWithApi:API_OrderPAY method:POST dic:dic successBlock:^(NSDictionary *dic) {
        if (self.paymentType==101) {
            NSString *appScheme=@"aliPay2088621752287648";
            [[AlipaySDK defaultService] payOrder:dic[@"data"] fromScheme:appScheme callback:^(NSDictionary *resultDic) {
            }];
            [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(pushClick) name:@"Ali_PaySuccess" object:nil];
            [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(pushOrderClickbtn) name:@"Ali_PayFairl" object:nil];
        }else if (self.paymentType==111){
            //NSLog(@"支付订单 %@",dic);
            NSData *jsonData = [dic[@"data"] dataUsingEncoding:NSUTF8StringEncoding];
            NSError *err;
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                                options:NSJSONReadingMutableContainers error:&err];
            
            if(err) {
                //NSLog(@"json解析失败：%@",err);
            }else{
                //NSLog(@"%@",dic);
                //调起微信支付
                PayReq* req             = [[PayReq alloc] init];
                req.openID              = [dic objectForKey:@"appid"];
                req.partnerId           = [dic objectForKey:@"partnerid"];
                req.prepayId            = [dic objectForKey:@"prepayid"];
                req.nonceStr            = [dic objectForKey:@"noncestr"];
                NSMutableString *stamp  = [dic objectForKey:@"timestamp"];
                req.timeStamp           = stamp.intValue;
                req.package             = [dic objectForKey:@"package"];
                req.sign                = [dic objectForKey:@"sign"];
                
                [WXApi sendReq:req];
                // 创建通知
                [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(pushClick) name:@"WX_PaySuccess" object:nil];
                [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(pushOrderClickbtn) name:@"WX_PayFairl" object:nil];
            }
            
        }else{
            
        }
        
    }];
    
}
- (void)pushClick
{
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    dic[@"orderSn"]=self.ordersn;
    //dic[@"paymentType"]=@(111);
    [self doHttpRequestWithApi:API_ConfirmPAY method:POST dic:dic successBlock:^(NSDictionary *dic) {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        ChargeResultTableViewController *vc=[[ChargeResultTableViewController alloc]init];
        vc.styple=1;
        [self.navigationController pushViewController:vc animated:YES];
        //NSLog(@"支付成功");
    }];
    
}
-(void)pushOrderClickbtn{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    ChargeResultTableViewController *vc=[[ChargeResultTableViewController alloc]init];
    vc.styple=2;
    [self.navigationController pushViewController:vc animated:YES];
    //NSLog(@"支付失败");
}
-(void)viewDidDisappear:(BOOL)animated{
    self.count.text=nil;
    [self.count resignFirstResponder];
}

@end
